﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_PersonEmployeeJobForm
 * 类型：       控制层
 * 功能说明：   人事系统-职位信息维护页面
 * 作者：       黄鑫
 * 创建日期：   2007/11/08
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using log4net;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;

public partial class App_Input_PersonEmployeeJobForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_PersonEmployeeJobForm));

    private const string SELECT_ALL = "-1";

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // 取得当前登录的用户id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;

            ViewState["USER_ID"] = loginId;
            ViewState["URL"] = Context.Items["URL"];
            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                ViewState["ParamPageIndex"] = Context.Items["ParamPageIndex"];
            }

            // 初始化控件
            initPageControls();

            if (Context.Items.Contains("ParamPageIndex"))
            {
                ViewState["EMPLOYEE_ID"] = Context.Items["EMPLOYEE_ID"];
            }
            else
            {
                ViewState["EMPLOYEE_ID"] = "-1";
                ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0039").ToString();
            }
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["CMB_GROUP_ID"] = Context.Items["CMB_GROUP_ID"];
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["CMB_JOB_ID"] = Context.Items["CMB_JOB_ID"];
            }
            if (Context.Items.Contains("CMB_STATUS_ID"))
            {
                this.ViewState["CMB_STATUS_ID"] = Context.Items["CMB_STATUS_ID"];
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_ID"))
            {
                this.ViewState["CMB_EMPLOYEE_ID"] = Context.Items["CMB_EMPLOYEE_ID"];
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_NAME"))
            {
                this.ViewState["CMB_EMPLOYEE_NAME"] = Context.Items["CMB_EMPLOYEE_NAME"];
            }
            if (Context.Items.Contains("EMPLOYEE_STATUS"))
            {
                if (Convert.ToString(Context.Items["EMPLOYEE_STATUS"]) == "-1")
                {
                    btnOk.Enabled = false;
                }
            }
            PersonEmployeeBean bean = new PersonEmployeeBean();
            PersonEmployeeDao dao = new PersonEmployeeDao();
            bean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
            bean = dao.Find(bean);
            if (bean == null)
            {
                return;
            }
            lblEmployeeId.Text = bean.EmployeeId;
            lblEmployeeName.Text = bean.EmployeeName;
            lblEmployeeDate.Text = bean.EmployeeDate == null ? "" : DateUtils.convertToString(bean.EmployeeDate.Value);
            lblGroupName.Text = bean.GroupName;
            lblJobName.Text = bean.JobName;
            if (bean.GroupId != null)
            {
                lstGroup.SelectedValue = bean.GroupId;
                setLstJob(bean.GroupId,"");
            }
            if (bean.JobId != null)
            {
                lstJob.SelectedValue = Convert.ToString(bean.JobId);
            }
        }
    }

    // 登记按钮
    protected void btnOk_Click(object sender, EventArgs e)
    {
        string employeeId = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
        PersonEmployeeBean bean = new PersonEmployeeBean();
        PersonEmployeeDao dao = new PersonEmployeeDao();
        bean.EmployeeId = employeeId;
        IList lstResult = dao.CheckEmployeeJob(bean);
        bean.GroupId = lstGroup.SelectedValue;
        bean.JobId = Convert.ToInt32(Request.Form["ctl00$ContentPlaceHolder1$lstJob"]);
        bean.UserId = Convert.ToString(this.ViewState["USER_ID"]);
        if (lstResult.Count == 0)
        {
            dao.InsertEmployeeJob(bean);
        }
        else
        {
            dao.UpdateEmployeeJob(bean);
        }

        PersonJobDao personJobDao = new PersonJobDao();
        PersonJobBean personJobBean = new PersonJobBean();
        IList list = null;

        personJobBean.DepartId = this.lstGroup.SelectedValue;
        list = personJobDao.ListResult(personJobBean);
        this.lstJob.DataSource = list;
        this.lstJob.DataBind();

        this.lstJob.SelectedValue = Request.Form["ctl00$ContentPlaceHolder1$lstJob"].ToString();
        ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0042").ToString();
    }


    // 返回按钮
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string directUrl = Convert.ToString(this.ViewState["URL"]);
        Context.Items["ParamPageIndex"] = this.ViewState["ParamPageIndex"];
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["CMB_GROUP_ID"]);
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["CMB_JOB_ID"]);
        Context.Items["CMB_STATUS_ID"] = Convert.ToString(this.ViewState["CMB_STATUS_ID"]);
        Context.Items["CMB_EMPLOYEE_ID"] = Convert.ToString(this.ViewState["CMB_EMPLOYEE_ID"]);
        Context.Items["CMB_EMPLOYEE_NAME"] = Convert.ToString(this.ViewState["CMB_EMPLOYEE_NAME"]);
        PersonEmployeeBean bean = new PersonEmployeeBean();
        PersonEmployeeDao dao = new PersonEmployeeDao();
        string employeeId = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
        bean.EmployeeId = employeeId;
        bean.EmployeeId = employeeId;
        bean = dao.Find(bean);
        Context.Items["EMPLOYEE_ID"] = bean.EmployeeId;
        Context.Items["PERSON_ID"] = Convert.ToString(bean.PersonId);
        ControllerUtils.Transfer(this.Context, directUrl, false);
    }


    // bind common catalog data 
    private void initPageControls()
    {
        PersonJobDao dao = new PersonJobDao();
        PersonJobBean bean = new PersonJobBean();
        IList lstJob1 = dao.ListJob(bean);
        
        IList lstGroup1 = dao.ListGroup(bean);
        

        // 职位列表
        lstJob.DataSource = lstJob1;
        lstJob.DataBind();

        // 部门列表
        lstGroup.DataSource = lstGroup1;
        lstGroup.DataBind();

    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        PersonEmployeeBean bean = new PersonEmployeeBean();
        PersonEmployeeDao dao = new PersonEmployeeDao();
        bean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
        bean = dao.Find(bean);
        if (bean == null)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0039").ToString();
            return;
        }
        lblEmployeeId.Text  = bean.EmployeeId;
        lblEmployeeName.Text = bean.EmployeeName;
        lblEmployeeDate.Text = bean.EmployeeDate == null ? "":DateUtils.convertToString(bean.EmployeeDate.Value);
        lblGroupName.Text = bean.GroupName;
        lblJobName.Text = bean.JobName;
    }

    //配置所属部门职位列表
    protected void setLstJob(string groupId, string jobId)
    {
        PersonJobDao dao = new PersonJobDao();
        PersonJobBean bean = new PersonJobBean();
        bean.GroupId = groupId;
        IList lstJob1 = dao.ListJob(bean);


        // 职位列表
        lstJob.DataSource = lstJob1;
        lstJob.DataBind();
        if (!string.IsNullOrEmpty(Convert.ToString(jobId)))
        {
            lstJob.SelectedValue = Convert.ToString(jobId);
        }
    }
}
